﻿namespace FFF.Test.DataAccess.Framework
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Diagnostics;
    using Microsoft.EntityFrameworkCore.Metadata;
    using FFF.Test.DataAccess.Framework.Configuration.Dto;
    using FFF.Test.Util.Consts;
    using Z.EntityFramework.Extensions;

    /// <summary>
    /// cmmand
    /// </summary>
    public class EFConnectionInterceptor : DbConnectionInterceptor
    {
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="connection">connection</param>
        /// <param name="interceptionContext">执行context</param>
        public override void Opening(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        {
            base.Opening(connection, interceptionContext);
            this.LogInfo("EFConnectionInterceptor.Opening", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 打开open
        /// </summary>
        /// <param name="connection">执行conection</param>
        /// <param name="interceptionContext">context对象</param>
        public override void Opened(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        {
            base.Opened(connection, interceptionContext);
            this.LogInfo("EFConnectionInterceptor.Opened", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 执行错误的
        /// </summary>
        /// <param name="connection">connection获取</param>
        /// <param name="interceptionContext">获取</param>
        /// <param name="exception">错误信息</param>
        public override void Error(DbConnection connection, DbConnectionInterceptionContext interceptionContext, Exception exception)
        {
            base.Error(connection, interceptionContext, exception);
            this.LogInfo("EFConnectionInterceptor.Error", interceptionContext.EventData.ToString(), exception.Message);
        }

        /// <summary>
        /// 关闭执行中
        /// </summary>
        /// <param name="connection">连接</param>
        /// <param name="interceptionContext">执行上下文</param>
        public override void Closing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        {
            base.Closing(connection, interceptionContext);
            this.LogInfo("EFConnectionInterceptor.Closing", interceptionContext.EventData.ToString());
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <param name="connection">连接</param>
        /// <param name="interceptionContext">context对象</param>
        public override void Closed(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        {
            base.Closed(connection, interceptionContext);
            this.LogInfo("EFConnectionInterceptor.Closed", interceptionContext.EventData.ToString());
        }

        private void LogInfo(string method, string data)
        {
            Console.WriteLine("Intercepted on: {0}: \n\t{1}", method, data);
        }

        private void LogInfo(string method, string data, string exception)
        {
            Console.WriteLine("Intercepted on: {0}: \n\t{1} \n\t{2}", method, data, exception);
        }
    }

}
